<?php
    class NoteController {
        private static
            $view;
        private
            $responses = array();
        const
            TEXT_EMPTY = 'Error. Textarea empty.',
            NO_ACCESS = 'Error. No access.';
        function __construct(){
            if (empty(self::$view)){
                self::$view = new NoteView;
            }
        }
        function getResponses(){
            return $this->responses;
        }
        function insert(){
            $userMapper = new UserMapper();
            $user = $userMapper->getCurrentUser();
            if (isset($user->id)){
                $note = new NoteModel(
                    array(
                        'userId' => $user->id,
                        'text' => mysql_real_escape_string($_POST['text'])
                    )
                );
                if (!empty($_POST['text'])){
                    $noteMapper = new NoteMapper();
                    $noteMapper->insert($note);
                    $this->responses = array_merge(
                        $this->responses,
                        $noteMapper->getResponses()
                    );
                } else {
                    $this->responses[] = self::TEXT_EMPTY;
                }
            } else {
                $this->responses[] = self::NO_ACCESS;
            }
            self::$view->render($this->responses);
        }
        function select(){
            $noteMapper = new NoteMapper();
            $notes = $noteMapper->selectNotes();
            self::$view->render($notes);
        }
    }
?>